﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BusPartRegister : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            this.btnSave.Enabled = false;
            this.btnAdd.Enabled = true;
            //load BusPartner List
            ProBPRegister bpr = new ProBPRegister();
            DataSet ds = bpr.LoadBPList();

            if (ds.Tables[0].Rows.Count == 0)
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                this.grdListDept.DataSource = ds.Tables[0];
                this.grdListDept.DataBind();
                this.grdListDept.Rows[0].Visible = false;
            }
            else
            {
                this.grdListDept.DataSource = ds.Tables[0];
                this.grdListDept.DataBind();
            }

            SetHeaderText();
        }
        else
        {
        }
    }

    public void SetHeaderText()
    {
        this.grdListDept.HeaderRow.Cells[2].Text = "BP Code";
        this.grdListDept.HeaderRow.Cells[3].Text = "Company";
        this.grdListDept.HeaderRow.Cells[4].Text = "Address";
        this.grdListDept.HeaderRow.Cells[5].Text = "Telephone";
        this.grdListDept.HeaderRow.Cells[6].Text = "Fax";
        this.grdListDept.HeaderRow.Cells[7].Text = "Representative";
        this.grdListDept.HeaderRow.Cells[8].Text = "Cell Phone";
        this.grdListDept.HeaderRow.Cells[9].Text = "Website";
        this.grdListDept.HeaderRow.Cells[10].Text = "Email";
        this.grdListDept.HeaderRow.Cells[11].Text = "Input by";
    }

    protected void ClearFields()
    {
        this.lblCode.Text = "";
        this.txtCompany.Text = "";
        this.txtAddress.Text = "";
        this.txtTel.Text = "";
        this.txtFax.Text = "";
        this.txtRep.Text = "";
        this.txtCellPhone.Text = "";
        this.txtWebsite.Text = "";
        this.txtEmail.Text = "";
        this.btnAdd.Enabled = true;
        this.btnSave.Enabled = false;
    }
    protected void grdListDept_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridView _gridView = (GridView)sender;

        // Get the selected index and the command name 
        int _selectedIndex = int.Parse(e.CommandArgument.ToString());
        string _commandName = e.CommandName;
        this.grdListDept.SelectedIndex = _selectedIndex;
    }
    protected void grdListDept_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.lblCode.Text = this.grdListDept.SelectedRow.Cells[2].Text;
        this.txtCompany.Text = this.grdListDept.SelectedRow.Cells[3].Text;
        this.txtAddress.Text = this.grdListDept.SelectedRow.Cells[4].Text;
        this.txtTel.Text = this.grdListDept.SelectedRow.Cells[5].Text;
        this.txtFax.Text = this.grdListDept.SelectedRow.Cells[6].Text;
        this.txtRep.Text = this.grdListDept.SelectedRow.Cells[7].Text;
        this.txtCellPhone.Text = this.grdListDept.SelectedRow.Cells[8].Text;
        this.txtWebsite.Text = this.grdListDept.SelectedRow.Cells[9].Text;
        this.txtEmail.Text = this.grdListDept.SelectedRow.Cells[10].Text;
        this.btnAdd.Enabled = false;
        this.btnSave.Enabled = true;
    }
    protected void grdListDept_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ProBPRegister pbp = new ProBPRegister();
        Boolean bResult = pbp.DeleteBusPartner(this.grdListDept.SelectedRow.Cells[2].Text);

        if (bResult)
            this.lblError.Text = Constant.MESSAGE_SUCESSFUL_DELETE_BPINFO;
        else
            this.lblError.Text = Constant.MESSAGE_ERROR_FAIL_DELETE_BPINFO;

        this.btnSave.Enabled = false;
        this.btnAdd.Enabled = true;
        //load BusPartner List
        DataSet ds = pbp.LoadBPList();

        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdListDept.DataSource = ds.Tables[0];
            this.grdListDept.DataBind();
            this.grdListDept.Rows[0].Visible = false;
        }
        else
        {
            this.grdListDept.DataSource = ds.Tables[0];
            this.grdListDept.DataBind();
        }

        SetHeaderText();
        ClearFields();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //insert to DB new BP
        BusinessInfo bsiBusInfo = new BusinessInfo();
        bsiBusInfo = FillBPInfoEntity();

        ProBPRegister pbp = new ProBPRegister();
        Boolean bResult = pbp.AddBusParter(bsiBusInfo);

        if (bResult)
            this.lblError.Text = Constant.MESSAGE_SUCSESSFUL_ADD_BPINFO;
        else
            this.lblError.Text = Constant.MESSAGE_ERROR_FAIL_ADD_BPINFO;

        this.btnSave.Enabled = false;
        this.btnAdd.Enabled = true;
        //load BusPartner List
        DataSet ds = pbp.LoadBPList();

        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdListDept.DataSource = ds.Tables[0];
            this.grdListDept.DataBind();
            this.grdListDept.Rows[0].Visible = false;
        }
        else
        {
            this.grdListDept.DataSource = ds.Tables[0];
            this.grdListDept.DataBind();
        }

        SetHeaderText();
        ClearFields();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //Update to DB BP
        BusinessInfo bsiBusInfo = new BusinessInfo();
        bsiBusInfo = FillBPInfoEntity();
        bsiBusInfo.StrBusPartCode = this.grdListDept.SelectedRow.Cells[2].Text;

        ProBPRegister bpr = new ProBPRegister();
        Boolean bResult = bpr.UpdateBusParter(bsiBusInfo);

        if (bResult)
            this.lblError.Text = Constant.MESSAGE_SUCSESSFUL_UPDATE_BPINFO;
        else
            this.lblError.Text = Constant.MESSAGE_ERROR_FAIL_UPDATE_BPINFO;

        this.btnSave.Enabled = false;
        this.btnAdd.Enabled = true;
        //load BusPartner List

        DataSet ds = bpr.LoadBPList();

        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdListDept.DataSource = ds.Tables[0];
            this.grdListDept.DataBind();
            this.grdListDept.Rows[0].Visible = false;
        }
        else
        {
            this.grdListDept.DataSource = ds.Tables[0];
            this.grdListDept.DataBind();
        }

        SetHeaderText();
        ClearFields();
    }

    public BusinessInfo FillBPInfoEntity()
    {
        ProBPRegister pbp = new ProBPRegister();
        BusinessInfo bsiBusInfo = new BusinessInfo();
        bsiBusInfo.StrCompany = this.txtCompany.Text;
        bsiBusInfo.StrAddress = this.txtAddress.Text;
        bsiBusInfo.StrBusPartCode = pbp.GenerateNewBPId();
        bsiBusInfo.StrCellPhone = this.txtCellPhone.Text;
        bsiBusInfo.StrEmail = this.txtCellPhone.Text;
        bsiBusInfo.StrFax = this.txtFax.Text;
        bsiBusInfo.StrRepresentative = this.txtRep.Text;
        bsiBusInfo.StrTel = this.txtTel.Text;
        bsiBusInfo.StrWebsite = this.txtWebsite.Text;
        bsiBusInfo.UiInputBy = new UserInfo();
        bsiBusInfo.UiInputBy.StrUserName = Session["user"].ToString();

        return bsiBusInfo;
    }
}
